package cn.djp.canting.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.djp.canting.R;
import cn.djp.canting.bean.OrderListBean;

//用于在用户个人中心展示订单信息的listview的adapter
public class OrderItemAdapter extends BaseAdapter {

    private List<OrderListBean> objects = new ArrayList<OrderListBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public OrderItemAdapter(Context context,List<OrderListBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public OrderListBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.order_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((OrderListBean) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(OrderListBean object, ViewHolder holder) {
        holder.goodsName.setText(object.getFoodName());
        holder.goodsContent.setText(object.getFoodContent());
        holder.allPrice.setText("共消费:"+object.getAllPrice()+"元");
        holder.goodsImg.setImageResource(object.getImgId());
    }

    protected class ViewHolder {
        private TextView goodsName;
        private TextView goodsContent;
        private TextView allPrice;
        private ImageView goodsImg;

        public ViewHolder(View view) {
            goodsName = (TextView) view.findViewById(R.id.goods_name);
            goodsContent = (TextView) view.findViewById(R.id.goods_content);
            allPrice = (TextView) view.findViewById(R.id.all_price);
            goodsImg = (ImageView) view.findViewById(R.id.goods_img);
        }
    }
}
